<?php
namespace pay;

class Api {
	public function wxscan($data) {
		$postdata=[
			'appid'=>'wx426b3015555a46be',
			'mch_id'=>'1900009851',
			'nonce_str'=>rand(1000,9999),
			'body'=>$data['body'],
			'out_trade_no'=>$data['out_trade_no'],
			'total_fee'=>'1',
			'spbill_create_ip'=>'127.0.0.1',
			'notify_url'=>'http://apidemo.cn/index/index/notify',
			'trade_type'=>'NATIVE',
			'product_id'=>'001',
		];
		ksort($postdata);
		$stringA='';
		foreach($postdata as $k=>$v) {			
			$stringA.=$k.'='.$v.'&';						
		}
		$stringA=trim($stringA,"&").'&key=8934e7d15453e97507ef794cf7b0519d';
		$stringA=md5($stringA);
		$postdata['sign']=strtoupper($stringA);
		$xmldata="
			<xml>
				<appid>wx426b3015555a46be</appid>
				<mch_id>1900009851</mch_id>
				<nonce_str>".$postdata['nonce_str']."</nonce_str>
				<body>".$postdata['body']."</body>
				<out_trade_no>".$postdata['out_trade_no']."</out_trade_no>
				<total_fee>1</total_fee>
				<spbill_create_ip>127.0.0.1</spbill_create_ip>
				<notify_url>http://apidemo.cn/index/index/notify</notify_url>
				<trade_type>NATIVE</trade_type>
				<product_id>001</product_id>
				<sign>".$postdata['sign']."</sign>
			</xml>
		";
		$ch=curl_init();
		curl_setopt($ch,CURLOPT_URL,'https://api.mch.weixin.qq.com/pay/unifiedorder');
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch,CURLOPT_POST,1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
		$response=curl_exec($ch);
		curl_close($ch);
		//halt($response);
		$res=simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
		$jsonres=json_encode($res);
		$arraydata=json_decode($jsonres,true);
		return $arraydata;
	}
}